package com.joy.controller;

import com.joy.command.*;
import com.joy.mapper.AssignmentMapper;
import com.joy.mapper.AssignmentSubmissionMapper;
import com.joy.model.Assignment;
import com.joy.model.AssignmentSubmission;
import com.joy.vo.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 任务相关接口
 */
@Controller
public class AssignmentController {


    @Resource
    private AssignmentMapper assignmentMapper;

    @Resource
    private AssignmentSubmissionMapper assignmentSubmissionMapper;

    /**
     * 发布任务
     */
    @PostMapping("/addAssignment")
    @ResponseBody
    public AddAssignmentVO addAssignment(@RequestBody AddAssignmentCommand command) {
        if (command.getId() == null) {
            return new AddAssignmentVO(false);
        }
        AddAssignmentFormCommand form = command.getForm();
        if (form == null || form.getName() == null) {
            return new AddAssignmentVO(false);
        }
        Assignment assignment = new Assignment();
        assignment.setCourseId(command.getId());
        assignment.setAssignmentName(form.getName());
        assignment.setDescription(form.getDesc());
        assignment.setPublishWay(form.getWay());
        assignment.setStartTime(form.getDate1());
        assignment.setEndTime(form.getDate2());
        assignmentMapper.insertAssignment(assignment);
        return new AddAssignmentVO(true);
    }

    /**
     * 查询课程下具体任务的所有提交
     */
    @PostMapping("/getAllAssignmentSub")
    @ResponseBody
    public AssignmentSubQueryVO getAllAssignmentSub(@RequestBody AssessmentSubQueryCommand command) {
        List<AssignmentSubmission> assignmentSubmissions = assignmentSubmissionMapper.selectByCourseIdAndAssignmentId(command.getTaskid(), command.getCourseid());
        if (assignmentSubmissions.isEmpty()) {
            return new AssignmentSubQueryVO();
        }
        List<AssignmentSubmissionVO> stu = new ArrayList<>();
        for (AssignmentSubmission assignmentSubmission : assignmentSubmissions) {
            stu.add(AssignmentSubmissionVO.from(assignmentSubmission));
        }
        return new AssignmentSubQueryVO(stu);
    }

    /**
     * 查询任务提交详情
     */
    @PostMapping("/getAssessmentSubDetail")
    @ResponseBody
    public AssessmentSubDetailVO getAssessmentSubDetail(@RequestBody AssignmentSubDetailCommand command) {
        AssignmentSubmission assignmentSubmission =
                assignmentSubmissionMapper.selectByCondition(command.getId(), command.getTaskid(), command.getCourseid());
        return AssessmentSubDetailVO.from(assignmentSubmission);
    }

    /**
     * 批改作业提交
     */
    @PostMapping("/correctAssessmentSub")
    @ResponseBody
    public CorrectAssessmentSubVO correctAssessmentSub(@RequestBody CorrectAssessmentSubCommand command) {
        if (command.getId() == null) {
            return new CorrectAssessmentSubVO(false);
        }
        AssignmentSubmission assignmentSubmission = new AssignmentSubmission();
        assignmentSubmission.setAssignmentId(command.getTaskid());
        assignmentSubmission.setCourseId(command.getCourseid());
        assignmentSubmission.setStudentId(command.getId());
        assignmentSubmission.setScore(command.getGrade());
        assignmentSubmission.setAssessment(command.getAssessment());
        assignmentSubmissionMapper.updateAssignmentSubmission(assignmentSubmission);
        return new CorrectAssessmentSubVO(true);
    }

    /**
     * 学生作业提交
     */
    @PostMapping("/finishAssessmentSub")
    @ResponseBody
    public FinishAssessmentSubVO finishAssessmentSub(@RequestBody FinishAssessmentSubCommand command) {
        if (command.getId() == null || command.getCourseid() == null || command.getTaskid() == null) {
            return new FinishAssessmentSubVO(false);
        }
        AssignmentSubmission assignmentSubmission = new AssignmentSubmission();
        assignmentSubmission.setAssignmentId(command.getTaskid());
        assignmentSubmission.setCourseId(command.getCourseid());
        assignmentSubmission.setStudentId(command.getId());
        assignmentSubmission.setAssessment(command.getContent());
        // 还需要落 任务名称、学生名称，去查询表
        assignmentSubmissionMapper.insertAssignmentSubmission(assignmentSubmission);
        return new FinishAssessmentSubVO(true);
    }
}
